package com.v1.video.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.OrientationEventListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.starschina.abs.media.ThinkoPlayerListener;
import com.starschina.sdk.player.ThinkoPlayerView;
import com.starschina.types.DChannel;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.adapter.AdImageAdapter;
import com.v1.video.adapter.CibnRecommendAdapter;
import com.v1.video.cibn.EventConst;
import com.v1.video.cibn.LoadingView;
import com.v1.video.cibn.SimpleEvent;
import com.v1.video.cibn.VideoCtrlView;
import com.v1.video.cibn.common.Epg.EpgEntity;
import com.v1.video.domain.CarouselInfo4;
import com.v1.video.domain.CibnEntity;
import com.v1.video.domain.CibnEntity.CibnEntityLst;
import com.v1.video.domain.CibnVideoDetailEntity;
import com.v1.video.domain.CibnVideoEntity;
import com.v1.video.domain.CibnVideoEntity.Anthologys;
import com.v1.video.domain.CibnVideoEntity.CibnVideoInfo;
import com.v1.video.domain.VideoAdEntity;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;
import com.v1.video.util.PopupWindowUtils;
import com.v1.video.util.PopupWindowUtils.OnActionSheetItemClick;
import com.v1.video.util.Utils;
import com.v1.video.view.CircleFlowIndicator;
import com.v1.video.view.ViewFlow;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class CibnVideoDetailActivity extends BaseActivity implements
		OnClickListener {

	//1、CIBN电视剧 2、CIBN综艺 3、CIBN电影 4、CIBN动漫
	public static final String CIBN_TELEPLAY = "1";
	public static final String CIBN_VARIETY = "2";
	public static final String CIBN_FILM = "3";
	public static final String CIBN_COMIC = "4";
	private View mViewHeader,mLayAnthology,mVLine,mVadLine;
	private ThinkoPlayerView mPlayerView;
	private TextView mTvTitle, mTvContent,mTvAnthology;
	private ImageView mIvMore;
	private ViewFlow mViewflow;
	private CircleFlowIndicator mFlowIndicator;
	private ListView mListView;
	private OrientationEventListener mOrientationListener;
	private boolean isPortrait = true;
	private LoadingView mLoadingView;
	private VideoCtrlView mCtrlView;
	private CibnVideoEntity mCibnVideoEntity;
	private CibnEntityLst mCibnEntityLst;
	private DChannel mChannel;
	private boolean mOnPrepared = false;
	private int mScreenWidth;
	private int mScreenHeight;
	private RecyclerView mRecyclerView;
	private RelativeLayout mLayAd;
	private AnthologyAdapter mAnthologyAdapter;
	private PopupWindow mPopupWindow;
	private String mGenre;
	private boolean mShowContent = false;
	private AdImageAdapter mImageAdapter;
	private CibnRecommendAdapter mRecommendAdapter;
	private String mCid;
	private int mCurIndex = 0;
	private String mVid;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		EventBus.getDefault().register(this);
		initOrientationListener();
		setContentView(R.layout.activity_cibnvideodetail);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mViewHeader = getLayoutInflater().inflate(
				R.layout.activity_cibnvideodetail_header, null);
		mListView = (ListView) findViewById(R.id.lv_recommend);
		mListView.addHeaderView(mViewHeader);
		mTvTitle = (TextView) mViewHeader.findViewById(R.id.tv_title);
		mIvMore = (ImageView) mViewHeader.findViewById(R.id.iv_more);
		mTvContent = (TextView) mViewHeader.findViewById(R.id.tv_content);
		mViewflow = (ViewFlow) mViewHeader.findViewById(R.id.vf_viewflow);
		mFlowIndicator = (CircleFlowIndicator) mViewHeader
				.findViewById(R.id.flowIndicator);
		mPlayerView = (ThinkoPlayerView) findViewById(R.id.player);
		mScreenHeight = Utils.getScreenHeight(this);
		mScreenWidth = Utils.getScreenWidth(this);
		int mHeight = (int) (mScreenWidth * 9 / 16);
		LayoutParams params = mPlayerView.getLayoutParams();
		params.height = mHeight;
		params.width = mScreenWidth;
		mPlayerView.setLayoutParams(params);
		mVLine = mViewHeader.findViewById(R.id.v_line);
		mLayAnthology = mViewHeader.findViewById(R.id.lay_anthology);
		mTvAnthology = (TextView) mViewHeader.findViewById(R.id.tv_anthology);
		mRecyclerView = (RecyclerView) mViewHeader.findViewById(R.id.rv_recyclerview);
		LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper.HORIZONTAL);
        mTvContent.setVisibility(View.GONE);
		mLayAd = (RelativeLayout) mViewHeader.findViewById(R.id.lay_ad);
		int height = (int) (mScreenWidth * 5 / 16);
		LayoutParams param = mLayAd.getLayoutParams();
		param.height = height;
		param.width = mScreenWidth;
		mLayAd.setLayoutParams(param);
		mVadLine = mViewHeader.findViewById(R.id.v_adline);
		initPlayer();
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		mIvMore.setOnClickListener(this);
		mTvAnthology.setOnClickListener(this);
		mListView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				switch (scrollState) {
				case OnScrollListener.SCROLL_STATE_IDLE:
					ImageLoader.getInstance().resume();
					break;
				case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					ImageLoader.getInstance().pause();
					break;
				case OnScrollListener.SCROLL_STATE_FLING:
					ImageLoader.getInstance().pause();
					break;
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_more:
			if(mShowContent){
				mShowContent = false;
				mTvContent.setVisibility(View.GONE);
				mIvMore.setImageResource(R.drawable.icon_cibn_deploy_default);
			} else {
				mShowContent = true;
				mTvContent.setVisibility(View.VISIBLE);
				mIvMore.setImageResource(R.drawable.icon_cibn_shrink_default);
			}
			break;
		case R.id.tv_anthology:
			if (mCibnVideoEntity != null && mCibnVideoEntity.getRows() != null
					&& mCibnVideoEntity.getRows().size() > 0) {
				int height = mScreenHeight - (int) (mScreenWidth * 9 / 16)
						- Utils.getStatusBarHeight(this);
				mPopupWindow = PopupWindowUtils.getInstance().showPopupWindow(
						this, v, height, mCibnVideoEntity.getRows(), mGenre,
						mCurIndex, new OnActionSheetItemClick() {

							@Override
							public void onClick(int index, String actionName) {
								// TODO Auto-generated method stub
								changeAnthology(index);
							}
						});
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		mVid = intent.getStringExtra("vid");
		mCid = intent.getStringExtra("cid");
		mGenre = intent.getStringExtra("genre");
		mAnthologyAdapter = new AnthologyAdapter(this);
	    mRecyclerView.setAdapter(mAnthologyAdapter);
		mRecommendAdapter = new CibnRecommendAdapter(this, mGenre);
		mListView.setAdapter(mRecommendAdapter);
		getVideoDetail();
	}
	
	private void refreshData(){
		if (mCibnVideoEntity != null) {
			if (mCibnVideoEntity.getRows() != null
					&& mCibnVideoEntity.getRows().size() > 0) {
				Anthologys anthologys = mCibnVideoEntity.getRows().get(0);
				setPlayerSource(anthologys, 0);
				if (CIBN_FILM.equals(mGenre)) {
					mVLine.setVisibility(View.GONE);
					mTvAnthology.setVisibility(View.GONE);
					mLayAnthology.setVisibility(View.GONE);
					mRecyclerView.setVisibility(View.GONE);
				} else {
					if (!TextUtils.isEmpty(mCibnVideoEntity.getIntro())) {
						mTvAnthology.setVisibility(View.VISIBLE);
						mTvAnthology.setText(mCibnVideoEntity.getIntro());
					} else {
						mVLine.setVisibility(View.GONE);
					}
					mVLine.setVisibility(View.VISIBLE);
					mAnthologyAdapter.setLstData(mCibnVideoEntity.getRows());
				}
			} else {
				mVLine.setVisibility(View.GONE);
				mTvAnthology.setVisibility(View.GONE);
				mLayAnthology.setVisibility(View.GONE);
				mRecyclerView.setVisibility(View.GONE);
			}
			if (!TextUtils.isEmpty(mCibnVideoEntity.getVideoName())) {
				mTvTitle.setText(mCibnVideoEntity.getVideoName());
			} else {
				mTvTitle.setText("");
			}
			String content = "";
			if (mCibnVideoEntity.getVideoInfo() != null
					&& mCibnVideoEntity.getVideoInfo().size() > 0) {
				for (int i = 0; i < mCibnVideoEntity.getVideoInfo().size(); i++) {
					CibnVideoInfo info = mCibnVideoEntity.getVideoInfo().get(i);
					if (info != null) {
						if (TextUtils.isEmpty(content)) {
							content = info.getTitle() + ":" + info.getValue();
						} else {
							content = content + "\n" + info.getTitle() + ":"
									+ info.getValue();
						}
					}
				}
			}
			mTvContent.setText(content);
		}
		if (mCibnEntityLst != null && mCibnEntityLst.getRows() != null) {
			if (mRecommendAdapter != null) {
				mRecommendAdapter.setLstData(mCibnEntityLst.getRows());
			}
		}
	}
	
	private void getVideoDetail(){
		String url = String.format(Constant.CIBN_VIDEODETAIL, mVid,
				Utils.getPhoneOsVersion());
		Logger.i(TAG, "获取CIBN详情url=" + url);
		RequestManager.getInstance().getRequest(this, url, CibnVideoDetailEntity.class,
				new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						// TODO Auto-generated method stub
						try {
							if (object != null) {
								CibnVideoDetailEntity entity = (CibnVideoDetailEntity) object;
								if (entity != null && entity.getBody() != null
										&& entity.getBody() != null) {
									mCibnVideoEntity = entity.getBody().getDes();
									mCibnEntityLst=entity.getBody().getRem();
									refreshData();
									getCarouselData();
								}
							}
						} catch (Exception ex) {
							ex.printStackTrace();
						}
					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						Logger.e(TAG, message);
					}

				});
	}
	
	private void getCarouselData() {
		String url = String.format(V1VideoHttpApi.ADVERTISEMENT_LIST(), "1");
		Logger.i(TAG, "获取广告url=" + url);
		RequestManager.getInstance().getRequest(this, url, VideoAdEntity.class,
				new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						// TODO Auto-generated method stub
						try {
							if (object != null) {
								VideoAdEntity entity = (VideoAdEntity) object;
								if (entity != null && entity.getBody() != null
										&& entity.getBody().getData() != null) {
									showViewFlow(entity.getBody().getData()
											.getCarousels());
								} else {
									hideViewFlow();
								}
							}
						} catch (Exception ex) {
							ex.printStackTrace();
						}
					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						Logger.e(TAG, message);
						hideViewFlow();
					}

				});
	}
	
	/**
	 * 显示轮播图
	 */
	private void showViewFlow(List<CarouselInfo4> lstCarousel) {
		if (lstCarousel != null && lstCarousel.size() > 0) {
			try {
				int perWidth = (int) this.getResources().getDimension(
						R.dimen.cf_indicator_per_width);
				int width = lstCarousel.size() * perWidth;
				ViewGroup.LayoutParams params= mFlowIndicator.getLayoutParams();
				params.width = width;
				mFlowIndicator.setLayoutParams(params);
				mImageAdapter = new AdImageAdapter(this);
				mImageAdapter.setLstData(lstCarousel);
				mViewflow.setAdapter(mImageAdapter);
				mViewflow.setmSideBuffer(lstCarousel.size());
				mViewflow.setFlowIndicator(mFlowIndicator);
				if (lstCarousel.size() > 1) {
					mViewflow.setSelection(lstCarousel.size() * 1000); // 设置初始位置
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} else {
			hideViewFlow();
		}
	}
	
	private void hideViewFlow(){
		mVadLine.setVisibility(View.GONE);
		mLayAd.setVisibility(View.GONE);
	}
	
	private void getRecommendData(){
		String url = String.format(Constant.CIBN_RECOMMEND_LIST, mCid, Utils.getPhoneOsVersion());
		Logger.i(TAG, "获取CIBN推荐url=" + url);
		RequestManager.getInstance().getRequest(this, url,
				CibnEntity.class, new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						// TODO Auto-generated method stub
						try {
							if (object != null) {
								CibnEntity entity = (CibnEntity) object;
								if (entity != null && entity.getBody()!=null && entity.getBody().getRows() != null
										&& entity.getBody().getRows().size() > 0) {
									if (mRecommendAdapter != null) {
										mRecommendAdapter.setLstData(entity.getBody().getRows());
									}
									
								}
							}
						} catch (Exception ex) {
							ex.printStackTrace();
						}
					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						Logger.e(TAG, message);
					}

				});
	}

	private void initPlayer() {
		mPlayerView.setBackgroundResource(R.color.black);
		mPlayerView.setPlayerListener(mListener);
		// 是否硬解,(开硬解，可解决偶尔绿屏)
		mPlayerView.useMediaCodec(true);
		// 自定义loading view
		mLoadingView = new LoadingView(this);
		mPlayerView.setLoadingView(mLoadingView);
		// 自定义播控界面
		mCtrlView = new VideoCtrlView(this, mPlayerView);
		mPlayerView.setMediaCtrlView(mCtrlView);
		mPlayerView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mCtrlView.isShown()) {
					mCtrlView.setVisibility(View.GONE);
				} else {
					mCtrlView.setVisibility(View.VISIBLE);
				}
			}
		});
	}
	
	private void setPlayerSource(Anthologys anthologys,int index) {
		if (anthologys != null) {
			mChannel = new DChannel();
			// mChannel.id = 1149427;
			mChannel.id = anthologys.getpUrlId();
			mChannel.name = anthologys.getTitle();
			mChannel.type = 0;
			mCurIndex = index;
			if (mCtrlView != null) {
				mCtrlView.setTitle(mChannel.name);
				mCtrlView.setVisibility(View.INVISIBLE);
				mCtrlView.setChannel(mChannel);
				// 直播
				if (mChannel.type == 1 && mChannel.currentEpg != null) {
					mCtrlView.setEpgTime(mChannel.currentEpg.startTime,
							mChannel.currentEpg.endTime);
				}
			}
			if (mAnthologyAdapter != null) {
				mAnthologyAdapter.notifyDataSetChanged();
			}
			if (mPlayerView != null) {
				mPlayerView.stop();
			}
			prepareToPlay();
		}
	}
	
	public void showRecommendVideo(String vid) {
		mVid = vid;
		getVideoDetail();
	}
	
	private void changeAnthology(int index){
		if (mCibnVideoEntity != null
				&& mCibnVideoEntity.getRows() != null
				&& mCibnVideoEntity.getRows().size() > index) {
			Anthologys anthologys = mCibnVideoEntity.getRows().get(index);
			setPlayerSource(anthologys, index);
		}
	}

	public void portrait() {
		isPortrait = true;
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		int mHeight = (int) (mScreenWidth * 9 / 16);
		LayoutParams params = mPlayerView.getLayoutParams();
		params.height = mHeight;
		params.width = mScreenWidth;
		mPlayerView.setLayoutParams(params);
		// findViewById(R.id.ad_container).setVisibility(View.VISIBLE);
		// if (mLiveReviewMenu != null && mLiveReviewMenu.isShown()) {
		// mLiveReviewMenu.hide();
		// mCtrlView.setVisibility(View.VISIBLE);
		// }
	}

	public void landscape() {
		isPortrait = false;
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		LayoutParams params = mPlayerView.getLayoutParams();
		params.height = mScreenWidth;
		params.width = mScreenHeight;
		mPlayerView.setLayoutParams(params);
		mListView.setVisibility(View.GONE);
		// mAd.setOrientation(1);//0:竖屏， 1：横屏
		// findViewById(R.id.ad_container).setVisibility(View.GONE);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (mPlayerView.onKeyDown(keyCode, event)) {
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (!isPortrait) {
				portrait();
				return true;
			}
			if (mPopupWindow != null && mPopupWindow.isShowing()) {
				mPopupWindow.dismiss();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	private ThinkoPlayerListener mListener = new ThinkoPlayerListener() {

		@Override
		public void onPrepared() {
			// TODO Auto-generated method stub
			Logger.i("demo", "onPrepared");
			mCtrlView.refreshPlayerStatus(true);
			mOnPrepared = true;
		}

		@Override
		public void onNetworkSpeedUpdate(int arg0) {
			// TODO Auto-generated method stub
			Logger.i("demo", "onNetworkSpeedUpdate");
		}

		@Override
		public boolean onInfo(int arg0, int arg1) {
			// TODO Auto-generated method stub
			Logger.i("demo", "onInfo:arg0:" + arg0);
			if (mOnPrepared) {
				if (arg0 == 701) {
					mCtrlView.refreshPlayerStatus(false);
				} else if (arg0 == 702) {
					mCtrlView.refreshPlayerStatus(true);
				}
			}
			return false;
		}

		@Override
		public boolean onError(int arg0, int arg1) {
			// TODO Auto-generated method stub
			prepareToPlay();
			return false;
		}

		@Override
		public void onCompletion() {
			// TODO Auto-generated method stub
			Logger.i("demo", "onCompletion");
		}

		@Override
		public void onBuffer(int arg0) {
			// TODO Auto-generated method stub
			Logger.i("demo", "onBuffer:" + arg0);
		}
	};

	private void initOrientationListener() {
//		mOrientationListener = new OrientationEventListener(this,
//				SensorManager.SENSOR_DELAY_NORMAL) {
//
//			@Override
//			public void onOrientationChanged(int orientation) {
//				// TODO Auto-generated method stub
//				if (orientation > 225 && orientation < 315) {
//					if (isPortrait) {
//						Logger.i(TAG, "切换成横屏");
//						landscape();
//					}
//				} else if (orientation > 45 && orientation < 135) {
//					if (isPortrait) {
//						Logger.i(TAG, "切换成横屏");
//						landscape();
//					}
//				} else if ((orientation > 315 && orientation < 360)
//						|| (orientation > 0 && orientation < 45)) {
//					if (!isPortrait) {
//						Logger.i(TAG, "切换成竖屏");
//						portrait();
//					}
//				}
//			}
//		};
	}
	
	private void prepareToPlay(){
		if (mChannel != null && mPlayerView != null) {
			mPlayerView.prepareToPlay(mChannel);
		}
	}
	
	private void pause(){
		if (mCtrlView != null) {
			mCtrlView.release();
		}
		if (mPlayerView != null) {
			mPlayerView.stop();
		}
	}
	
	private void destroy(){
		if (mPlayerView != null) {
			mPlayerView.release();
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEventFromPlayer(SimpleEvent event) {
		switch (event.mType) {
		// 回看
		case EventConst.EVENT_LIVE_SEEK_BY_EPG:
			EpgEntity epg = (EpgEntity) event.mObj;
			if (epg.getStartTime() > System.currentTimeMillis() / 1000) {
				showToast("只能回看已播放了的节目!");
				return;
			}
			// epgstarttime = epg.getStartTime();
			// epgendtime = epg.getEndTime();
			// 回看要重新加载播放中广告
			// mPlayerView.ifShowPlayingAd(true);
			mPlayerView.seekByEpg(epg.getStartTime(), true);
			mCtrlView.refreshPlayerStatus(false);
			mCtrlView.setEpgTime(epg.getStartTime(), epg.getEndTime());
			mCtrlView.showToLive();
			// if (mLiveReviewMenu != null) {
			// mLiveReviewMenu.hide();
			// mCtrlView.setVisibility(View.VISIBLE);
			// }
			break;
		// 时移
		case EventConst.EVENT_LIVE_SEEK:
			// 时移不用重新加载播放中广告
			// mPlayerView.ifShowPlayingAd(false);
			Logger.d("demo", "seek time:" + event.mObj);
			mPlayerView.seekByEpg((Integer) event.mObj, false);
			mCtrlView.refreshPlayerStatus(false);
			mCtrlView.showToLive();
			break;
		case EventConst.EVENT_UPDATE_EPG:
			EpgEntity curEpg = (EpgEntity) event.mObj;
			mCtrlView.setEpgTime(curEpg.getStartTime(), curEpg.getEndTime());
			break;
		case EventConst.EVENT_EXIT:
			if (isPortrait) {
				finish();
			} else {
				portrait();
			}
			break;
		// 回到直播
		case EventConst.EVENT_TO_LIVE:
			mPlayerView.toLive();
			mCtrlView.setEpgTime(mChannel.currentEpg.startTime,
					mChannel.currentEpg.endTime);
			break;
		case EventConst.EVENT_PORTRAIT:
			mListView.setVisibility(View.VISIBLE);
			portrait();
			break;
		case EventConst.EVENT_LANDSCAPE:
			landscape();
			break;
		case EventConst.EVENT_SHOW_LIVE_REVIEW:
			// showLiveReview();
			mCtrlView.setVisibility(View.GONE);
			break;
		case EventConst.EVENT_UPDATE_NEXT_EPG:
			EpgEntity next = (EpgEntity) event.mObj;
			mChannel.nextEpg.id = next.getEpgId();
			mChannel.nextEpg.name = next.getEpgName();
			mChannel.nextEpg.startTime = next.getStartTime();
			mChannel.nextEpg.endTime = next.getEndTime();
			break;
		case EventConst.EVENT_SHOW_NEXT_EPG:
			mCtrlView.setEpgTime(mChannel.nextEpg.startTime,
					mChannel.nextEpg.endTime);
			break;
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (mOrientationListener != null) {
			mOrientationListener.enable();
		}
		prepareToPlay();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (mOrientationListener != null) {
			mOrientationListener.disable();
		}
		pause();
	}
	

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// mAd.release();
		destroy();
		if (mPopupWindow != null) {
			if (mPopupWindow.isShowing()) {
				mPopupWindow.dismiss();
			}
			mPopupWindow = null;
		}
		EventBus.getDefault().unregister(this);
	}
	
	
	public class AnthologyAdapter extends RecyclerView.Adapter<AnthologyAdapter.ViewHolder> {

        private List<Anthologys> mLstData;
        private Context mContext;
        private int mWidth;

        public AnthologyAdapter(Context context) {
            mContext = context;
            mLstData = new ArrayList<Anthologys>();
			if (CibnVideoDetailActivity.CIBN_VARIETY.equals(mGenre)) {
				mWidth = (int) context.getResources().getDimension(
						R.dimen.control_size_96);
			} else {
				mWidth = (int) context.getResources().getDimension(
						R.dimen.control_size_48);
			}
        }

        public void setLstData(List<Anthologys> mLstData) {
            if (mLstData != null && mLstData.size() > 0) {
                this.mLstData = mLstData;
                this.notifyDataSetChanged();
            }
        }

        @Override
        public int getItemCount() {
            return mLstData.size();
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            final Anthologys info = mLstData.get(position);
			final int index = position;
			if (position == 0) {
				holder.layLeft.setVisibility(View.VISIBLE);
			} else {
				holder.layLeft.setVisibility(View.GONE);
			}
			if (CibnVideoDetailActivity.CIBN_VARIETY.equals(mGenre)) {
				if (!TextUtils.isEmpty(info.getTitle())) {
					holder.name.setText(info.getTitle());
				} else {
					holder.name.setText("");
				}
			} else {
				if (!TextUtils.isEmpty(info.getSort())) {
					holder.name.setText(info.getSort());
				} else {
					holder.name.setText("");
				}
			}
			if (position == mCurIndex) {
				holder.image.setVisibility(View.VISIBLE);
				holder.name.setVisibility(View.GONE);
			} else {
				holder.image.setVisibility(View.GONE);
				holder.name.setVisibility(View.VISIBLE);
			}
            holder.layItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!Helper.checkConnection(mContext)) {
                        Toast.makeText(mContext, R.string.net_no_notclick,
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
					if (mCurIndex != index) {
						changeAnthology(index);
					}
                }
            });
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View view = View.inflate(mContext,
					R.layout.activity_cibnvideodetail_anthology_item, null);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            private View layItem,layLeft;
            private ImageView image;
            private TextView name;

            public ViewHolder(View itemView) {
                super(itemView);
				layItem = itemView.findViewById(R.id.lay_item);
				layLeft = itemView.findViewById(R.id.v_left);
				image = (ImageView) itemView.findViewById(R.id.iv_image);
				name = (TextView) itemView.findViewById(R.id.tv_name);
				LayoutParams params = layItem.getLayoutParams();
				params.width = mWidth;
				layItem.setLayoutParams(params);
            }
        }
    }

}
